package com.yykj.drup.server.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yykj.common.vo.ResultVO;
import com.yykj.drup.server.form.DoctorProductFrom;
import com.yykj.drup.server.model.Product;
import com.yykj.drup.server.param.ProductNameParam;
import com.yykj.drup.server.param.ProductParam;
import com.yykj.drup.server.service.DoctorProductService;
import com.yykj.drup.server.service.ProductService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author zeng
 * @Date 2022/3/9
 */
@RestController
@Api(tags = "药品")
@RequestMapping("/api/drug")
@AllArgsConstructor
public class ProductController {

    final ProductService productService;
    final DoctorProductService doctorProductService;

    @ApiOperation("根据药品分类查询药品列表")
    @PostMapping("/product/findByCategoryId")
    @ApiResponses({
            @ApiResponse(code = 0,message = "",response = Product.class)
    })
    public ResultVO findByCategoryId(@RequestBody ProductParam param){
        IPage<Product> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        return ResultVO.success(productService.findProductPageByParam(param, page));
    }

    @ApiOperation("根据药品名称查询药品列表")
    @PostMapping("/product/findByProductName")
    public ResultVO findByProductName(@RequestBody ProductNameParam param){
        Assert.notEmpty(param.getCategoryType(),"中西药标识参数错误");
        IPage<Product> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        return ResultVO.success(productService.findProductPageByNameParam(param,page));
    }

    @ApiOperation("获取药品列表(后台)")
    @GetMapping("/product/selectProductName")
    @ApiResponses({
            @ApiResponse(code = 0,message = "",response = Product.class)
    })
    @ApiModelProperty(name = "productName",value = "药品名称")
    public ResultVO selectProductName(@RequestParam(value = "productName",required = false) String productName){

        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(productName),"title",productName);
        List<Product> list = productService.list(wrapper);
        return ResultVO.success(list);
    }


    @ApiOperation("根据药品Id查询")
    @GetMapping("/product/findById")
    @ApiModelProperty(name = "id",value = "药品系统主键ID")
    @ApiResponses({
            @ApiResponse(code = 0,message = "",response = Product.class)
    })
    public ResultVO findById(@RequestParam(name = "id") Integer id){
        return ResultVO.success(productService.getById(id));
    }

    @ApiOperation("添加医生常用药品")
    @PostMapping("/product/addOften")
    public ResultVO addOften(@RequestBody DoctorProductFrom from){
        try {
            doctorProductService.setDoctorProductCount(from.getDoctorId(),from.getCategoryType(),from.getProductIds());
            return ResultVO.success();
        }catch (RuntimeException e){
            return ResultVO.error(e.getMessage());
        }
    }

    @ApiOperation("获取医生常用药品列表")
    @GetMapping("/product/findByDoctorId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId",value = "医生主键ID",required = true),
            @ApiImplicitParam(name = "categoryType",value = "中西药标识ID 1：西 2：中",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 0,message = "",response = Product.class)
    })
    public ResultVO findByDoctorId(
            @RequestParam(name = "doctorId") String doctorId,
            @RequestParam(name = "categoryType") Integer categoryType
    ){
        return ResultVO.success(doctorProductService.findProductsByDoctorId(doctorId,categoryType));
    }
}
